package Huihu.src.Duoxiancheng;
/*
设计4个线程，其中两个线程每次对j增加1，另外两个线程对j每次减少1。考虑到线程的安全性写出程序。
 */
public class Anquan {
    public static void main(String[] args) {
        jia jia1=new jia();
        jia jia2=new jia();
        new Thread(jia1,"jias").start();
        new Thread(jia2,"jiaes").start();

        jian jian1=new jian();
        jian jian2=new jian();
        new Thread(jian1,"jians").start();
        new Thread(jian2,"jianes").start();
    }
}
class jia implements Runnable{

    @Override
    public synchronized void run() {
        for (int j = 0; j < 10; j++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+j);
        }
    }
}

class jian implements Runnable{

    @Override
    public synchronized void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int j = 10; j >0; j--) {
            System.out.println(Thread.currentThread().getName()+j);
        }
    }
}
